<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>渐变线条</title>
    <style>
        body{
            height: 100vh;
            width: 100vw;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        canvas{
            border: dashed 2px #ccc;
        }
    </style>
    <script>
        function $$(id){
          return document.getElementById(id);
        }
        function pageLoad(){
            let can = $$('can');
            let cans = can.getContext('2d');
            cans.moveTo(0,0);
            cans.lineTo(400,300);
            let gnt1 = cans.createLinearGradient(0,0,400,300);
            gnt1.addColorStop(0,'red');
            gnt1.addColorStop(1,'yellow');
            cans.lineWidth = 4;
            cans.strokeStyle = gnt1;
            cans.stroke();
        }
    </script>
</head>
<body onload="pageLoad()">
    <canvas id="can" width="400px" height="300px">4</canvas>
</body>
</html>